package com.smec.remes.module.user.adapter;

import android.app.Activity;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hwangjr.rxbus.Bus;
import com.hwangjr.rxbus.SmecRxBus;
import com.smec.moblie.BR;
import com.smec.moblie.R;
import com.smec.moblie.databinding.LayoutNullDataBinding;
import com.smec.moblie.databinding.LayoutRecyclerviewUserBinding;
import com.smec.remes.base.SmecBaseRecyclerViewHolder;
import com.smec.remes.bean.GetUserName;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/12/5.
 */

public class SmecUserAdapter extends RecyclerView.Adapter<SmecBaseRecyclerViewHolder> {
    private static int  NULLDATA = 0;
    private static int  DATA = 1;
    private Context mcontext;
    protected Bus mRxbus;
    private ArrayList<GetUserName> arrayList=new ArrayList<>();

    public SmecUserAdapter(Context context) {
        this.mcontext=context;
        mRxbus= SmecRxBus.get();
        mRxbus.register(this);
    }

    public void _addList(ArrayList<GetUserName> list,String status){
        if(status.equals("onRefresh")){
            arrayList=list;
        }else {
            arrayList.addAll(list);
        }

        notifyDataSetChanged();
    }

    public void _clearList(){
        arrayList.clear();
        notifyDataSetChanged();
    }

    @Override
    public SmecBaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == DATA){
            LayoutRecyclerviewUserBinding layoutRecyclerviewUserBinding= DataBindingUtil.inflate
                    (LayoutInflater.from(parent.getContext()), R.layout.layout_recyclerview_user,parent,false);
            SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                    SmecBaseRecyclerViewHolder(layoutRecyclerviewUserBinding.getRoot());
            smecBaseRecyclerViewHolder.setViewType(viewType);
            smecBaseRecyclerViewHolder.setBinding(layoutRecyclerviewUserBinding);
            return smecBaseRecyclerViewHolder;
        }else {
            LayoutNullDataBinding layoutNullDataBinding = DataBindingUtil.inflate
                    (LayoutInflater.from(parent.getContext()), R.layout.layout_null_data,parent,false);
            SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                    SmecBaseRecyclerViewHolder(layoutNullDataBinding.getRoot());
            smecBaseRecyclerViewHolder.setViewType(viewType);
            smecBaseRecyclerViewHolder.setBinding(layoutNullDataBinding);
            return smecBaseRecyclerViewHolder;
        }
    }

    @Override
    public void onBindViewHolder(SmecBaseRecyclerViewHolder holder, int position) {
        if(holder.getViewType() == DATA) {
            final LayoutRecyclerviewUserBinding layoutRecyclerviewUserBinding= (LayoutRecyclerviewUserBinding) holder.getBinding();
            layoutRecyclerviewUserBinding.setVariable(BR.user,arrayList.get(position));
            layoutRecyclerviewUserBinding.llUser.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String name=layoutRecyclerviewUserBinding.tvUsername.getText().toString();
                    Activity activity = (Activity)mcontext;
                    activity.finish();
                    mRxbus.post("UserActivity",name);

                }
            });
        }
        holder.getBinding().executePendingBindings();
    }

    @Override
    public int getItemViewType(int position) {
        if(arrayList == null || arrayList.size() == 0){
            return NULLDATA;
        }else{
            return DATA;
        }
    }

    @Override
    public int getItemCount() {
        if(arrayList.size() == 0){
            return 1;
        }else{
            return arrayList.size();
        }
    }
}
